/**************************************************************
 *
 * Copyright (c) 2008, ******, All Rights Reserved.
 *
 * 名 称:
 *		str.cpp
 * 位 置:
 *		src/pub/str.cpp
 * 描 述:
 *		公用字符串类
 * 备 注:
 *		无
 * 修 改:
 *		2008-09-14  Simon   - 创建
 *
 **************************************************************/

extern "C"
{
#include <string.h>
#include <regex.h>
}
#include <string>
#include <iostream>
#include <sstream>
#include "pub/log.h"
#include "pub/str.h"

using namespace std;

String::String() :
	string()
{
}
String::String(const String& str) :
	string((string) str)
{
}
String::String(const char* str) :
	string(str)
{
}
String::String(const char* str, unsigned len) :
	string(str, len)
{
}
String::String(const string& str) :
	string(str)
{
}

//----------------------------------------------
//	裁剪右侧空格(C字符串)
//----------------------------------------------
String& String::rtrim(char chr)
{
	erase(find_last_not_of(chr) + 1);
	return *this;
}

//----------------------------------------------
//	裁剪左侧空格(C字符串)
//----------------------------------------------
String& String::ltrim(char chr)
{
	erase(0, find_first_not_of(chr));
	return *this;
}

//----------------------------------------------
//	裁剪两侧空格(C字符串)
//----------------------------------------------
String& String::trim(char chr)
{
	return ltrim(chr).rtrim(chr);
}

//----------------------------------------------
//	填补右侧空格(C字符串)
//----------------------------------------------
String& String::rpad(unsigned len, char chr)
{
	if (len <= length())
		return *this;

	len -= length();
	insert(end(), len, chr);

	return *this;
}

//----------------------------------------------
//	填补左侧空格(C字符串)
//----------------------------------------------
String& String::lpad(unsigned len, char chr)
{
	if (len <= length())
		return *this;

	len -= length();
	insert(begin(), len, chr);

	return *this;
}

//----------------------------------------------
//	左补零
//----------------------------------------------
String& String::lpadz(unsigned len)
{
	trim().lpad(len, 0x30);
	return *this;
}

//----------------------------------------------
//	左补空格
//----------------------------------------------
String& String::lpads(unsigned len)
{
	trim().lpad(len, 0x20);
	return *this;
}

//----------------------------------------------
//	右补空格
//----------------------------------------------
String& String::rpads(unsigned len)
{
	trim().rpad(len, 0x20);
	return *this;
}

//----------------------------------------------
//	转换为大写
//----------------------------------------------
String& String::ucase()
{
	String::iterator it;
	for (it = begin(); it != end(); it++)
	{
		if (*it >= 'a' && *it <= 'z')
			*it += ('A' - 'a');
	}

	return *this;
}

//----------------------------------------------
//	转换为小写
//----------------------------------------------
String& String::lcase()
{
	String::iterator it;
	for (it = begin(); it != end(); it++)
	{
		if (*it >= 'A' && *it <= 'A')
			*it += ('a' - 'A');
	}

	return *this;
}

//----------------------------------------------
//	将字符串转换为字符数组,裁剪空格
//----------------------------------------------
String& String::tcopy(char* arr, unsigned len)
{
	memset(arr, 0x00, len);
	trim();
	copy(arr, len - 1, 0);

	return *this;
}

//----------------------------------------------
//	将字符数组转换为字符串,裁剪空格
//----------------------------------------------
String& String::tassign(char* arr, unsigned len)
{
	arr[len - 1] = 0x00;
	assign(arr, strlen(arr));
	trim();

	return *this;
}

//----------------------------------------------
//	将C字符串转换为字符串,裁剪空格
//----------------------------------------------
String& String::tassign(const char* arr)
{
	assign(arr, strlen(arr));
	trim();

	return *this;
}

//----------------------------------------------
//	字符串转换为整型
//----------------------------------------------
int String::toi()
{
	int i;
	stringstream ss((string) (*this));
	ss >> i;

	return i;
}

//----------------------------------------------
//	字符串转换为长整型
//----------------------------------------------
long String::tol()
{
	int l;
	stringstream ss((string) (*this));
	ss >> l;

	return l;
}

//----------------------------------------------
//	整型转换为字符串
//----------------------------------------------
String& String::fromi(int i)
{
	stringstream ss;
	ss << i;
	assign(ss.str());

	return *this;
}

//----------------------------------------------
//	长整型转换为字符串
//----------------------------------------------
String& String::froml(long l)
{
	stringstream ss;
	ss << l;
	assign(ss.str());

	return *this;
}

//----------------------------------------------
//	匹配正则表达式
//----------------------------------------------
bool String::match(const char* pattern)
{
	regex_t reg;
	int ret = 0;

	ret = regcomp(&reg, pattern, REG_NOSUB | REG_EXTENDED);
	if (ret != 0)
	{
		Log::error("正则表达式规则错误 %s", pattern);
		regfree(&reg);
		return false;
	}

	ret = regexec(&reg, this->c_str(), 0, 0, 0);
	if (ret == REG_NOMATCH)
	{
		Log::error("正则表达式验证错误 [%s]=~/%s/", this->c_str(), pattern);
		regfree(&reg);
		return false;
	}

	regfree(&reg);
	return true;
}

String& String::operator=(const char* src)
{
	string::operator=(src);
	return *this;
}

String& String::operator=(const String& src)
{
	string::operator=(src);
	return *this;
}
String operator+(const String& str1, const char* str2)
{
	return operator+((string) str1, str2);
}
String operator+(const char* str1, const String& str2)
{
	return operator+(str1, (string) str2);
}
String operator+(const String& str1, const String& str2)
{
	return operator+((string) str1, (string) str2);
}
bool operator==(const String& str1, const char* str2)
{
	return operator==((string) str1, str2);
}
bool operator==(const char* str1, const String& str2)
{
	return operator==(str1, (string) str2);
}
bool operator==(const String& str1, const String& str2)
{
	return operator==((string) str1, (string) str2);
}
bool operator!=(const String& str1, const char* str2)
{
	return operator!=((string) str1, str2);
}
bool operator!=(const char* str1, const String& str2)
{
	return operator!=(str1, (string) str2);
}
bool operator!=(const String& str1, const String& str2)
{
	return operator!=((string) str1, (string) str2);
}
